package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.MapsTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1PolyMorph;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.RandomArrayList;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.World;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ExcavateTable {
  private static final Log _log = LogFactory.getLog(ExcavateTable.class);
  
  private static ArrayList<ArrayList<Object>> _array = new ArrayList<>();
  
  private static final String TOKEN = ",";
  
  private static ExcavateTable _instance;
  
  public static ExcavateTable get() {
    if (_instance == null)
      _instance = new ExcavateTable(); 
    return _instance;
  }
  
  private ExcavateTable() {
    PerformanceTimer timer = new PerformanceTimer();
    getData();
    _log.info("载入挖矿游戏设置资料数量: " + _array.size() + "(" + timer.get() + "ms)");
    if (_array.size() <= 0) {
      _array.clear();
      _array = null;
    } 
  }
  
  public static void forExcavate(L1PcInstance pc) {
    if (_array.size() <= 0)
      return; 
    int MapId = pc.getMapId();
    ArrayList<?> aTempData = null;
    int i = 0;
    while (i < _array.size()) {
      aTempData = _array.get(i);
      if (check(pc) && ((Integer)aTempData.get(0)).intValue() == MapId) {
        if (((Integer)aTempData.get(1)).intValue() != 0 && 
          pc.getLevel() < ((Integer)aTempData.get(1)).intValue()) {
          pc.sendPackets(
              (ServerBasePacket)new S_SystemMessage("你的等级不足" + ((Integer)aTempData.get(1)).intValue() + "，以至于挖不到任何东西"));
          return;
        } 
        if (((Integer)aTempData.get(2)).intValue() != 0 && 
          !pc.getInventory().checkItem(((Integer)aTempData.get(2)).intValue())) {
          L1Item temp = ItemTable.get().getTemplate(((Integer)aTempData.get(2)).intValue());
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("必需要有(" + temp.getName() + ")才挖的到东西。"));
          return;
        } 
        int[] giveItem = (int[])aTempData.get(4);
        int[] giveCount = (int[])aTempData.get(5);
        int rndItem = RandomArrayList.getInt(giveItem.length);
        int rndCount = RandomArrayList.getInt(giveCount.length);
        int giveItemGet = giveItem[rndItem];
        int giveCountGet = giveCount[rndCount];
        int[] giveItem2 = (int[])aTempData.get(7);
        int[] giveCount2 = (int[])aTempData.get(8);
        int rndItem2 = RandomArrayList.getInt(giveItem2.length);
        int rndCount2 = RandomArrayList.getInt(giveCount2.length);
        int giveItemGet2 = giveItem2[rndItem2];
        int giveCountGet2 = giveCount2[rndCount2];
        int[] giveItem3 = (int[])aTempData.get(10);
        int[] giveCount3 = (int[])aTempData.get(11);
        int rndItem3 = RandomArrayList.getInt(giveItem3.length);
        int rndCount3 = RandomArrayList.getInt(giveCount3.length);
        int giveItemGet3 = giveItem3[rndItem3];
        int giveCountGet3 = giveCount3[rndCount3];
        int[] giveItem4 = (int[])aTempData.get(13);
        int[] giveCount4 = (int[])aTempData.get(14);
        int rndItem4 = RandomArrayList.getInt(giveItem4.length);
        int rndCount4 = RandomArrayList.getInt(giveCount4.length);
        int giveItemGet4 = giveItem4[rndItem4];
        int giveCountGet4 = giveCount4[rndCount4];
        int[] giveItem5 = (int[])aTempData.get(16);
        int[] giveCount5 = (int[])aTempData.get(17);
        int rndItem5 = RandomArrayList.getInt(giveItem5.length);
        int rndCount5 = RandomArrayList.getInt(giveCount5.length);
        int giveItemGet5 = giveItem5[rndItem5];
        int giveCountGet5 = giveCount5[rndCount5];
        if (((Integer)aTempData.get(2)).intValue() != 0) {
          L1PolyMorph.doPoly((L1Character)pc, 3642, 8, 2);
          pc.sendPackets((ServerBasePacket)new S_DoActionGFX(pc.getId(), 7));
          try {
            Thread.sleep(10000L);
          } catch (InterruptedException e) {
            e.printStackTrace();
          } 
          if (RandomArrayList.getInc(100, 1) <= ((Integer)aTempData.get(3)).intValue()) {
            L1ItemInstance item = ItemTable.get().createItem(giveItemGet);
            if (item.isStackable()) {
              item.setCount(giveCountGet);
            } else {
              item.setCount(1L);
            } 
            if (item != null) {
              if (pc.getInventory().checkAddItem(item, giveCountGet) == 0) {
                pc.getInventory().storeItem(item);
              } else {
                World.get().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
              } 
              pc.sendPackets(
                  (ServerBasePacket)new S_SystemMessage("\\aE铿、铿.似乎挖到矿石[" + item.getName() + " " + giveCountGet + "]"));
            } 
          } else if (RandomArrayList.getInc(100, 1) <= ((Integer)aTempData.get(6)).intValue()) {
            L1ItemInstance item = ItemTable.get().createItem(giveItemGet2);
            if (item.isStackable()) {
              item.setCount(giveCountGet2);
            } else {
              item.setCount(1L);
            } 
            if (item != null) {
              if (pc.getInventory().checkAddItem(item, giveCountGet2) == 0) {
                pc.getInventory().storeItem(item);
              } else {
                World.get().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
              } 
              pc.sendPackets((ServerBasePacket)new S_SystemMessage(
                    "\\aI铿、铿.挖到不错的矿石[" + item.getName() + " " + giveCountGet2 + "]"));
            } 
          } else if (RandomArrayList.getInc(100, 1) <= ((Integer)aTempData.get(9)).intValue()) {
            L1ItemInstance item = ItemTable.get().createItem(giveItemGet3);
            if (item.isStackable()) {
              item.setCount(giveCountGet3);
            } else {
              item.setCount(1L);
            } 
            if (item != null) {
              if (pc.getInventory().checkAddItem(item, giveCountGet3) == 0) {
                pc.getInventory().storeItem(item);
              } else {
                World.get().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
              } 
              pc.sendPackets((ServerBasePacket)new S_SystemMessage(
                    "\\aD铿、挖到有点价值的矿石[" + item.getName() + " " + giveCountGet3 + "]"));
            } 
          } else if (RandomArrayList.getInc(100, 1) <= ((Integer)aTempData.get(12)).intValue()) {
            L1ItemInstance item = ItemTable.get().createItem(giveItemGet4);
            if (item.isStackable()) {
              item.setCount(giveCountGet4);
            } else {
              item.setCount(1L);
            } 
            if (item != null) {
              if (pc.getInventory().checkAddItem(item, giveCountGet4) == 0) {
                pc.getInventory().storeItem(item);
              } else {
                World.get().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
              } 
              pc.sendPackets((ServerBasePacket)new S_SystemMessage(
                    "\\aD铿、挖到价值不斐的矿石[" + item.getName() + " " + giveCountGet4 + "]"));
            } 
          } else if (RandomArrayList.getInc(100, 1) <= ((Integer)aTempData.get(15)).intValue()) {
            L1ItemInstance item = ItemTable.get().createItem(giveItemGet5);
            if (item.isStackable()) {
              item.setCount(giveCountGet5);
            } else {
              item.setCount(1L);
            } 
            if (item != null) {
              if (pc.getInventory().checkAddItem(item, giveCountGet5) == 0) {
                pc.getInventory().storeItem(item);
              } else {
                World.get().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
              } 
              pc.sendPackets(
                  (ServerBasePacket)new S_SystemMessage("\\aD铿、挖到黄金矿石[" + item.getName() + " " + giveCountGet5 + "]"));
            } 
          } else {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fW什么都没有挖到。"));
          } 
        } 
        pc.setSkillEffect(10010, 3000);
      } 
      i++;
    } 
  }
  
  public static void ExcavateKnow(L1PcInstance pc) {
    ArrayList<?> aTempData = null;
    pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前可以挖掘的地区有："));
    int i = 0;
    while (i < _array.size()) {
      aTempData = _array.get(i);
      pc.sendPackets((ServerBasePacket)new S_SystemMessage(MapsTable.get().getLoaction(((Integer)aTempData.get(0)).intValue())));
      i++;
    } 
  }
  
  private void getData() {
    Connection cn = null;
    Statement ps = null;
    ResultSet rset = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.createStatement();
      rset = ps.executeQuery("SELECT * FROM w_挖掘系统");
      while (rset.next()) {
        ArrayList<Object> aReturn = new ArrayList();
        aReturn.add(0, new Integer(rset.getInt("Map_Id")));
        aReturn.add(1, new Integer(rset.getInt("Level")));
        aReturn.add(2, new Integer(rset.getInt("Check_Item")));
        aReturn.add(3, new Integer(rset.getInt("Random")));
        aReturn.add(4, getArray(rset.getString("Give_Item"), ",", 1));
        aReturn.add(5, getArray(rset.getString("Give_Item_Count"), ",", 1));
        aReturn.add(6, new Integer(rset.getInt("Random1")));
        aReturn.add(7, getArray(rset.getString("Give_Item1"), ",", 1));
        aReturn.add(8, getArray(rset.getString("Give_Item_Count1"), ",", 1));
        aReturn.add(9, new Integer(rset.getInt("Random2")));
        aReturn.add(10, getArray(rset.getString("Give_Item2"), ",", 1));
        aReturn.add(11, getArray(rset.getString("Give_Item_Count2"), ",", 1));
        aReturn.add(12, new Integer(rset.getInt("Random3")));
        aReturn.add(13, getArray(rset.getString("Give_Item3"), ",", 1));
        aReturn.add(14, getArray(rset.getString("Give_Item_Count3"), ",", 1));
        aReturn.add(15, new Integer(rset.getInt("Random4")));
        aReturn.add(16, getArray(rset.getString("Give_Item4"), ",", 1));
        aReturn.add(17, getArray(rset.getString("Give_Item_Count4"), ",", 1));
        _array.add(aReturn);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rset);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  private static boolean check(L1PcInstance tgpc) {
    try {
      if (tgpc == null)
        return false; 
      if (tgpc.getOnlineStatus() == 0)
        return false; 
      if (tgpc.getNetConnection() == null)
        return false; 
      if (tgpc.isTeleport())
        return false; 
    } catch (Exception e) {
      return false;
    } 
    return true;
  }
  
  private static Object getArray(String s, String sToken, int iType) {
    StringTokenizer st = new StringTokenizer(s, sToken);
    int iSize = st.countTokens();
    String sTemp = null;
    if (iType == 1) {
      int[] iReturn = new int[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        iReturn[i] = Integer.parseInt(sTemp);
        i++;
      } 
      return iReturn;
    } 
    if (iType == 2) {
      String[] sReturn = new String[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        sReturn[i] = sTemp;
        i++;
      } 
      return sReturn;
    } 
    if (iType == 3) {
      String sReturn2 = null;
      int i = 0;
      while (i < iSize) {
        sTemp = sReturn2 = st.nextToken();
        i++;
      } 
      return sReturn2;
    } 
    return null;
  }
  
  public static ArrayList<ArrayList<Object>> getSetList() {
    return _array;
  }
}
